package org.cwnuit.train.service;

import java.util.ArrayList;
import java.util.List;

import org.cwnuit.train.dao.LearnDAO;
import org.cwnuit.train.dao.RulesDAO;
import org.cwnuit.train.dao.TrainsDAO;
import org.cwnuit.train.dao.UsersDAO;
import org.cwnuit.train.dao.WtypeDAO;
import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.domain.Wtype;
import org.cwnuit.train.utils.PageModel;

@SuppressWarnings("all")
public class UsersService {
	
	TrainsDAO trainDAO = new TrainsDAO();
	UsersDAO usersDAO = new UsersDAO();
	RulesDAO rulesDAO = new RulesDAO();
	LearnDAO learnDAO = new LearnDAO();
	WtypeDAO wtypeDAO = new WtypeDAO();

	//添加用户信息
	public boolean addUser(String SQL) {
		return usersDAO.addUser(SQL);
	}
	
	//修改用户信息
	public boolean updateUser(String SQL) {
		return usersDAO.updateUser(SQL);
	}
	
	//删除用户信息
	public boolean deleteUser(String SQL) {
		return usersDAO.deleteUser(SQL);
	}
	
	//查询所有用户信息
	public List<Users> findUsers(String SQL) {
		return usersDAO.findUsers(SQL);
	}
	
	//查询用户信息
	public Users findUser(String SQL) {
		return usersDAO.findUser(SQL);
	}
	
	//分页查询用户信息
	public PageModel findByPage(int offset, int pagesize, String SQL) {
		PageModel pm = usersDAO.findByPage(offset, pagesize, SQL);
		List<Users> userlist = pm.getDatas();
		List<Users> userlistnew = new ArrayList();
		int totalnum = rulesDAO.findRules("SELECT * FROM Rules WHERE rlevel = 4").size();
		for(Users user : userlist){
			int pnum = learnDAO.findLearns("SELECT * FROM Learns WHERE uid = " + user.getUid()).size();
			Trains trains = (Trains)trainDAO.findTrain("SELECT * FROM Trains WHERE tid = " + user.getTid());
			user.setReadnum(pnum+"/"+totalnum);
			user.setTname(trains.getTname());
			if(user.getWid()!=0){
				Wtype wtype = (Wtype)wtypeDAO.findWtype("SELECT * FROM Wtype WHERE wid = " + user.getWid());
				user.setWname(wtype.getWname());
			}
			userlistnew.add(user);
		}
		pm.setDatas(userlistnew);
		return pm;
	}
	
	//用户登录
	public Users userLogin(String uname,String upass) {
		return usersDAO.userLogin(uname, upass);
	}
	

	//查询会员信息并导出为Excel
	public List findUsersToExcel(List<Users> users) {
		
		List content = new ArrayList();//全部内容
		List title = new ArrayList();//Excel标题
		title.add("车站用户信息导出");
		List column = new ArrayList();//Excel列明
		String header[] = { "用户编号","车站名称","真实姓名","用户名","登陆密码","角色名称"};
		for(int i = 0 ;i<header.length;i++)
		{
			column.add(header[i]);
		}
		content.add(title);
		content.add(column);
		
		for (Users user : users) {
			List data = new ArrayList();//查询内容
			data.add(user.getUid());
			data.add(trainDAO.findTrain("SELECT * FROM Trains WHERE tid = "+user.getTid()).getTname());
			data.add(user.getUface());
			data.add(user.getUname());
			data.add(user.getUpass());
			if(0==user.getUrole()){data.add("超级管理员");}
			else if(1==user.getUrole()){data.add("段长组");}
			else if(2==user.getUrole()){data.add("审核组");}
			else if(3==user.getUrole()){data.add("编制组");}
			else if(4==user.getUrole()){data.add("车站组");}
			else if(5==user.getUrole()){data.add("学习用户");}
			content.add(data);
		}
		return content;
	}

}
